package cn.tedu.csmall.stock.service.impl;


import cn.tedu.csmall.commons.exception.CoolSharkServiceException;
import cn.tedu.csmall.commons.pojo.stock.dto.StockReduceCountDTO;
import cn.tedu.csmall.commons.restful.ResponseCode;

import cn.tedu.csmall.stock.mapper.StockMapper;
import cn.tedu.csmall.stock.service.IStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class StockServiceImpl implements IStockService {
    @Autowired
    private StockMapper stockMapper;

    @Override
    public void reduceStockByCommodityCode(StockReduceCountDTO stockReduceCountDTO) {
        // 调用mapper层的减少库存方法
        //参数1：商品编号
        //参数2：购买的商品数量-->要减少的库存数
        int rows = stockMapper.updateStockByCommodityCode(
                stockReduceCountDTO.getCommodityCode(),
                stockReduceCountDTO.getReduceCount()
        );
        //如果rows==0,就是没有减少库存
        if (rows == 0)
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST, "库存不足");
        log.info("库存减少完成！调用服务器的端口是：{}");
    }
}
